import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:lovify/core/constants/config.dart';
import 'package:lovify/models/enums/gender.dart';
import 'package:lovify/models/user.dart';
import 'package:lovify/state/user_controller.dart';
import 'package:lovify/widgets/aq_avatar.dart';
import 'package:lovify/widgets/aq_button.dart';
import 'package:lovify/widgets/aq_cake.dart';
import 'package:lovify/widgets/aq_gender_switch.dart';
import 'package:lovify/widgets/aq_input.dart';
import 'package:lovify/widgets/aq_scroll_body.dart';

class EditScreen extends StatefulWidget {
  const EditScreen({super.key});

  @override
  State<EditScreen> createState() => _EditScreenState();
}

class _EditScreenState extends State<EditScreen> {

  bool isLoading = false;
  final _userState = Get.find<UserController>();

  Future<void> _doAction() async {
    setState(() {
      isLoading = true;
    });

    bool status = await _userState.saved();

    setState(() {
      isLoading = false;
    });

    if (status) {
      Navigator.pop(context as dynamic);
    }
  }

  @override
  Widget build(BuildContext context) {
    User u = _userState.user.value;
    return AQScrollBody(
      isLoading: isLoading,
      children: [
        AqAvatar(
          radius: 48,
          imageUrl: previewUrl + (u.avatar as String),
        ),
        const SizedBox(height: 24),
        AQInput(
          label: "姓名",
          defaultValue: u.name as String,
          placeholder: "请填写你的姓名",
          prefixIcon: Icons.person,
          onChanged: (value, _) => {
            _userState.setName(value)
          },
        ),
        const SizedBox(height: 12),
        AQCake(
          label: "生日",
          placeholder: "请选择你的生日",
          defaultValue: u.birthday,
          prefixIcon: Icons.cake,
          suffixIcon: Icons.calendar_today,
          onChanged: (value) => {
            _userState.setBirthday(value)
          },
        ),
        const SizedBox(height: 24),
        AqGenderSwitch(
          defaultValue: u.gender,
          onChanged: (value) => {
            _userState.setGender(valueOfGender(value))
          },
        ),
        const SizedBox(height: 24),
        AQButton(
          label: "编辑资料",
          onPressed: () {
            _doAction();
          },
        ),
      ],
    );
  }
}
